/*******************************************************************************
 * Copyright (c) 2014 lifeng (lifengsofts@gmail.com)
 * See the file licenses/license.txt for copying permission.
 ******************************************************************************/
package cn.woblog.android.httputils.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpUriRequest;

import cn.woblog.android.httputils.domain.Request;
import cn.woblog.android.httputils.exception.AppException;
import cn.woblog.android.httputils.exception.AppException.Type;

public class HttpURLConnectUtils {
	public static HttpURLConnection execute(Request request)
			throws AppException {
		switch (request.method) {
		case GET:
			return get(request);
		case POST:
			return post(request);
		case PUT:

			break;
		case DELETE:

			break;

		default:
			throw new IllegalStateException("the request method "
					+ request.method + " can't be support!");
		}
		return null;
	}

	private static HttpURLConnection post(Request request) throws AppException {
		try {
			URL url = new URL(request.url);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(Consts.CONNECTION_TIMEOUT);
			conn.setReadTimeout(Consts.SO_TIMEOUT);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			setHeaders(conn, request.headers);
			OutputStream outputStream = conn.getOutputStream();
			if (request.entiry != null) {
				request.entiry.writeTo(outputStream);
				// outputStream.flush();
				// outputStream.close();
			}
			if (request.callback != null) {
				boolean isClosed = request.callback
						.onCustomParams(outputStream);
				if (!isClosed) {
					outputStream.flush();
					outputStream.close();
				}
			}
			return conn;
		} catch (MalformedURLException e) {
			throw new AppException(Type.MalformedURLException, e.getMessage());
		} catch (ProtocolException e) {
			throw new AppException(Type.ProtocolException, e.getMessage());
		} catch (IOException e) {
			throw new AppException(Type.IOException, e.getMessage());
		}
	}

	private static HttpURLConnection get(Request request) throws AppException {
		try {
			URL url = new URL(request.url);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(Consts.CONNECTION_TIMEOUT);
			conn.setReadTimeout(Consts.SO_TIMEOUT);
			conn.setDoInput(true);
			setHeaders(conn, request.headers);
			return conn;
		} catch (MalformedURLException e) {
			throw new AppException(Type.MalformedURLException, e.getMessage());
		} catch (ProtocolException e) {
			throw new AppException(Type.ProtocolException, e.getMessage());
		} catch (IOException e) {
			throw new AppException(Type.IOException, e.getMessage());
		}
	}

	private static void setHeaders(HttpURLConnection conn,
			Map<String, String> headers) {
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				conn.addRequestProperty(entry.getKey(), entry.getValue());
			}
		}
	}
}
